package com.zzw.springboot.service.impl;


import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zzw.springboot.bean.Product;
import com.zzw.springboot.bean.SysUser;
import com.zzw.springboot.mapper.ProductMapper;
import com.zzw.springboot.mapper.SysUserMapper;
import com.zzw.springboot.service.ProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 商品服务实现类
 */
@Service
public class ProductServiceImpl extends ServiceImpl<ProductMapper, Product> implements ProductService {

    @Autowired
    private ProductMapper productMapper;

    @Override
    @Transactional
    public boolean addProduct(Product product) {
        // 检查商品编码是否已存在
        if (checkProductCodeExists(product.getProductCode(), null)) {
            return false;
        }
        return productMapper.insertProduct(product) > 0;
    }

    @Override
    @Transactional
    public boolean updateProduct(Product product) {
        // 检查商品编码是否已存在（排除自身）
        if (checkProductCodeExists(product.getProductCode(), product.getProductId())) {
            return false;
        }
        return productMapper.updateProduct(product) > 0;
    }

    @Override
    @Transactional
    public boolean deleteProduct(Integer productId) {
        return productMapper.deleteProduct(productId) > 0;
    }

    @Override
    @Transactional
    public boolean deleteProductBatch(Integer[] productIds) {
        return productMapper.deleteProductBatch(productIds) > 0;
    }

    @Override
    public Product getProductById(Integer productId) {
        return productMapper.selectProductById(productId);
    }

    @Override
    public List<Product> getProductList(Product product) {
        return productMapper.selectProductList(product);
    }

    @Override
    public List<Product> getAllProducts() {
        return productMapper.selectAll();
    }

    @Override
    public boolean checkProductCodeExists(String productCode, Integer productId) {
        return productMapper.checkProductCodeExists(productCode, productId) > 0;
    }
}
